/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jFrConta.java
 *
 * Created on 08/10/2009, 14:01:08
 */

package jbanco;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Cláudio
 */
public class jFrConta extends javax.swing.JFrame {

    public ArrayList<conta> Conta;
    public ArrayList<Cliente> cliente;
    private DefaultTableModel modelo;
    public boolean flag = true;




    /** Creates new form jFrConta */
    public jFrConta(ArrayList<conta> acc, ArrayList<Cliente> cli) {
        initComponents();
        Conta = acc;
        cliente = cli;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jBtnSair = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jBtnRemover = new javax.swing.JButton();
        jBtnEditar = new javax.swing.JButton();
        jBtnNovo = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jBtnDepositar = new javax.swing.JButton();
        jBtnSacar = new javax.swing.JButton();

        jInternalFrame1.setVisible(true);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 18));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/coins-128x128.png"))); // NOI18N
        jLabel1.setText("CONTAS");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Número", "Títular", "Tipo", "Saldo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(table);

        jBtnSair.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/filex_exit.gif"))); // NOI18N
        jBtnSair.setText("Sair");
        jBtnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSairActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Controlar Contas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 12))); // NOI18N

        jBtnRemover.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/error.png"))); // NOI18N
        jBtnRemover.setText("Remover");
        jBtnRemover.setPreferredSize(new java.awt.Dimension(83, 31));
        jBtnRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnRemoverActionPerformed(evt);
            }
        });

        jBtnEditar.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/edit_22x22.png"))); // NOI18N
        jBtnEditar.setText("Editar");
        jBtnEditar.setPreferredSize(new java.awt.Dimension(83, 31));
        jBtnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEditarActionPerformed(evt);
            }
        });

        jBtnNovo.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/07_icon_new_account.gif"))); // NOI18N
        jBtnNovo.setText("Nova");
        jBtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addComponent(jBtnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBtnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBtnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnNovo))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gerenciar Contas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 12))); // NOI18N

        jBtnDepositar.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnDepositar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/money.gif"))); // NOI18N
        jBtnDepositar.setText("Depositar");
        jBtnDepositar.setPreferredSize(new java.awt.Dimension(83, 31));
        jBtnDepositar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDepositarActionPerformed(evt);
            }
        });

        jBtnSacar.setFont(new java.awt.Font("Calibri", 0, 12));
        jBtnSacar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jbanco/sacar.png"))); // NOI18N
        jBtnSacar.setText("Deduzir");
        jBtnSacar.setPreferredSize(new java.awt.Dimension(83, 31));
        jBtnSacar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSacarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBtnDepositar, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addComponent(jBtnSacar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSacar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnDepositar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBtnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 162, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnSair))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNovoActionPerformed
        // TODO add your handling code here:
        Novo();
    }//GEN-LAST:event_jBtnNovoActionPerformed

    private void jBtnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSairActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jBtnSairActionPerformed

    private void jBtnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEditarActionPerformed
        // TODO add your handling code here:
        editar();
    }//GEN-LAST:event_jBtnEditarActionPerformed

    private void jBtnDepositarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnDepositarActionPerformed
        // TODO add your handling code here:
        flag = true;
        ReceberValor();


    }//GEN-LAST:event_jBtnDepositarActionPerformed

    private void jBtnRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnRemoverActionPerformed
        remover();
    }//GEN-LAST:event_jBtnRemoverActionPerformed

    private void jBtnSacarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSacarActionPerformed
        // TODO add your handling code here:
        flag = false;
        ReceberValor();
    }//GEN-LAST:event_jBtnSacarActionPerformed

// <editor-fold defaultstate="collapsed" desc="Metodos Privados">
    private void Novo() {
        jFrContaDados jrf = new jFrContaDados(this, true, null, cliente);
        jrf.setVisible(true);
        conta c = jrf.getConta();
        if (c == null) {
            return;
            
        }
        Conta.add(c);
        carregar();
    }

    private void carregar() {
        modelo = (DefaultTableModel) table.getModel();
        int qtdLinhas = modelo.getRowCount();
        if (qtdLinhas > 0) {
            for (int i = 0; i < qtdLinhas; i++) {
                modelo.removeRow(0);

                
            }
            
        }
        for (conta p : Conta) {
            Object[] dados = new Object[4];
            dados[0] = p.getNumero();
            dados[1] = p.getTitular();
            dados[2] = p.getTipo() == TipoPessoa.Fisica ? "Física" : "Jurídica";
            dados[3] = "R$ " + p.getSaldo();
            modelo.addRow(dados);
        }
    }

    private void ReceberValor() {
        int linha = table.getSelectedRow();
        if (linha < 0) {
            return;
            
        }
        jFrDepositar jfr = new jFrDepositar(this, true, Conta.get(linha),flag);
        jfr.setLocationRelativeTo(this);
        jfr.setVisible(true);
        Conta.set(linha, jfr.getValor());
        if (Conta.get(linha) == null) {
            return;
            
        }
        carregar();
    }

    private void remover() {
        if (JOptionPane.showConfirmDialog(this, "Deseja realmente remover o Funcionário?", "Atenção", 0, JOptionPane.WARNING_MESSAGE) != 0) {
            return;
            
        }
        int linha = table.getSelectedRow();
        if (linha < 0) {
            return;
            
        }
        Conta.remove(linha);
        carregar();

    }

    private void editar(){
        int linha = table.getSelectedRow();
        if(linha < 0)
            return;
        jFrContaDados jfr = new jFrContaDados(this, true,Conta.get(linha), cliente);
        jfr.setVisible(true);
        Conta.set(linha, jfr.getConta());
        if(Conta.get(linha) == null)
            return;
        carregar();
    }
    // </editor-fold>

    public boolean getFlag() {
        return flag;
    }


       
    




    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnDepositar;
    private javax.swing.JButton jBtnEditar;
    private javax.swing.JButton jBtnNovo;
    private javax.swing.JButton jBtnRemover;
    private javax.swing.JButton jBtnSacar;
    private javax.swing.JButton jBtnSair;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

}
